import Utils from "../../../Utils";
import NuclearBomb from "../../flyer/DestructibleFlyer/FlyerMissile/NuclearBomb";
import { Killer } from "../../funcObj/interface/HealthPoint";
import GameManager from "../../manager/GameManager";
import { Location } from "../../info/Predefine";
import NodePoolManager from "../../manager/NodePoolManager";
import Player from "../../player/Player";
import LaunchableFalicity from "../LaunchableFalicity";

const { ccclass, property } = cc._decorator;

/**
 * 核弹基地
 */
@ccclass
export default class NuclearBombBase extends LaunchableFalicity {

    @property(cc.Node)
    point: cc.Node = null;

    @property(cc.Sprite)
    base: cc.Sprite = null;

    @property(cc.Sprite)
    ready: cc.Sprite = null;

    @property([cc.SpriteFrame])
    baseFrames: cc.SpriteFrame[] = [];

    @property([cc.SpriteFrame])
    readyFrames: cc.SpriteFrame[] = [];

    nuclearBombCount: number;

    creating: boolean;

    get count(): number {
        return this.nuclearBombCount;
    }

    init(player: Player): void {
        super.init(player);
        this.base.spriteFrame = this.baseFrames[player.playerId];
        this.ready.spriteFrame = this.readyFrames[player.playerId];
        this.ready.node.active = false;
        this.nuclearBombCount = 0;
        this.creating = false;
        this.creat();
    }

    creat() {
        if (this.creating) return;
        this.CreatingProgress.progress = 0;
        this.CreatingProgress.node.active = true;
        this.creating = true;
        cc.tween(this.CreatingProgress)
            .to(this.makingTime, { progress: 1 })
            .call(() => {
                this.nuclearBombCount++;
                this.ready.node.active = true;
                this.player.node.emit(Player.EventType.更新飞行物数量, this.facilityType, 1);
            }).call(() => {
                this.CreatingProgress.node.active = false;
                this.creating = false;
                if (this.nuclearBombCount < this.facilityInfo.每基地可制造上限) {
                    this.creat();
                }
            })
            .start();
    }

    dispatch(target: Location) {
        super.dispatch(target);
        this.nuclearBombCount--;
        this.player.node.emit(Player.EventType.更新飞行物数量, this.facilityType, -1);
        let flyerParent = this.player.flyerParentNode;
        let nuclearBomb = NodePoolManager.instance.obtain(NodePoolManager.instance.nuclearBomb);

        this.dispatchQueue.push({
            flyer: nuclearBomb, target: target, dispatchFunc: (target: Location) => {
                this.ready.node.active = false;
                nuclearBomb.parent = flyerParent;
                nuclearBomb.position = flyerParent.convertToNodeSpaceAR(this.point.convertToWorldSpaceAR(cc.Vec3.ZERO));
                nuclearBomb.getComponent(NuclearBomb).init(this, target);
            }
        })

        this.creat();
    }


    die(killer: Killer): void {
        super.die(killer);
        this.unscheduleAllCallbacks();
        this.nuclearBombCount = 0;
        this.player.node.emit(Player.EventType.更新飞行物数量, this.facilityType, -1);
    }
}
